package com.hmdp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmdp.dto.Result;
import com.hmdp.dto.UserDTO;
import com.hmdp.entity.Follow;
import com.hmdp.mapper.FollowMapper;
import com.hmdp.service.IFollowService;
import com.hmdp.service.IUserService;
import com.hmdp.utils.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements IFollowService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private IUserService userService;

    @Override
    public Result follow(Long followUserId, Boolean isFollow) {

        //获取当前用户id
        Long userId = UserHolder.getUser().getId();

        //设置key
        String key = "follow:" + userId;

        //1.判断是否关注
        if (!isFollow) {
            //2.没有关注,尝试关注
            Follow follow = Follow.builder()
                    .userId(userId)
                    .followUserId(followUserId)
                    .build();
            //2.1新增用户关注记录
            boolean isSuccess = save(follow);
            //2.2向redis中存入关注列表
            if (isSuccess) {
                stringRedisTemplate.opsForSet().add(key, followUserId.toString());
            }
        } else {
            //3.已经关注，取消关注
            LambdaQueryWrapper<Follow> wrapper = new LambdaQueryWrapper<Follow>().eq(Follow::getUserId, userId).eq(Follow::getFollowUserId, followUserId);
            remove(wrapper);
            //3.2redis中移除记录
            stringRedisTemplate.opsForSet().remove(key, followUserId.toString());
        }

        return Result.ok();
    }


    /**
     * 判断是否关注
     *
     * @param followUserId
     * @return
     */
    @Override
    public Result isFollow(Long followUserId) {
        // 获取当前用户id
        Long userId = UserHolder.getUser().getId();
        // 查询关注表中是否存在记录
        Integer count = query().eq("user_id", userId).eq("follow_user_id", followUserId).count();
        return Result.ok(count != 0);
    }

    @Override
    public Result followCommons(Long followUserId) {
        //1.共同关注,userId+followUserId座位两个列表的key
        Long key = UserHolder.getUser().getId();
        //2.使用set集合中查询交集的部分
        Set<String> intersect = stringRedisTemplate.opsForSet().intersect(key.toString(), followUserId.toString());
        //2.1id字符串转Long
        List<Long> ids = intersect.stream().map(Long::valueOf).collect(Collectors.toList());

        //3.1Id集合转为UserDTO
        List<UserDTO> userDTOList = userService.listByIds(ids).stream()
                .map(user -> BeanUtil.copyProperties(user, UserDTO.class)).collect(Collectors.toList());

        //3.返回共同关注好友

        return Result.ok(userDTOList);

    }
}
